package Aleehot100;

import java.util.Arrays;

/**
 * ClassName: c04
 * Package: Aleehot100
 * Description:
 *
 * @Author BCXJ
 * @Create 2025/5/21 08:22
 * @Version 1.0
 * @Since 1.0
 */
public class c04 {
    public static void main(String[] args) {
        int[] arr = {0, 1, 0, 3, 12};
        moveZeroes(arr);

        System.out.println(Arrays.toString(arr));

    }


    /**
     * 双指针技巧，很值得深思
     * @param nums
     */
    public static void moveZeroes(int[] nums) {
        int l = 0, r = 0;

        for (r = 0; r < nums.length; r++) {
            if (nums[r] != 0) {
                swap(nums, l, r);
                l ++;
            }
        }


    }

    private static void swap(int[] nums, int l, int r) {
        if (l == r) {
            return;
        }
        nums[l] = nums[l] ^ nums[r];
        nums[r] = nums[l] ^ nums[r];
        nums[l] = nums[l] ^ nums[r];
    }

}
